<?php
/*class ShopProduct{
    public $title = 'default product';
    public $producerMainName ;
    public $producerFirstName;
}
$product1 = new ShopProduct();
$product1->producerSecoundName = '第三个名字';*/
/*var_dump($product1->title);
var_dump($product1->producerMainName);
var_dump($porduct1->producerSecoundName);*/

/*class ShopProduct{
    public $title = 'default product';
    public $producerMainName ;
    public $producerFirstName;
    public $numPages;
    public $playLength;
    public $price;

    public function __construct($title,$mainName,$firstName,$numPage,$playLength,$price)
    {
        $this->title = $title;
        $this->producerFirstName = $firstName;
        $this->producerMainName = $mainName;
        $this->numPages = $numPage;
        $this->playLength = $playLength;
        $this->price = $price;
    }

    public function getProducer(){
        return $this->producerFirstName.$this->producerMainName;
    }

    public function getSummaryLine(){
        $base = $this->title.'('.$this->producerFirstName.$this->producerMainName.')';
        return $base;
    }

}

class CdProduct extends ShopProduct{
    function getPlayLength(){
        return $this->playLength;
    }

    public function getSummaryLine(){
        $base = $this->title.'('.$this->producerFirstName.$this->producerMainName.') : playing time'.$this->playLength;
        return $base;
    }
}

class BookProduct extends ShopProduct{
    function getNumOfPages(){
        return $this->numPages;
    }

    public function getSummaryLine(){
        $base = $this->title.'('.$this->producerFirstName.$this->producerMainName.') : page count'.$this->numPages;
        return $base;
    }
}*/

//修改例子，让每个类只处理自己的数据

class ShopProduct{
    public $title = 'default product';
    public $producerMainName ;
    public $producerFirstName;
    private $price;
    public $discount = 0;

    public function __construct($title,$mainName,$firstName,$price)
    {
        $this->title = $title;
        $this->producerFirstName = $firstName;
        $this->producerMainName = $mainName;
        $this->price = $price;
    }

    public function getProducer(){
        return $this->producerFirstName.$this->producerMainName;
    }

    public function getSummaryLine(){
        $base = $this->title.'('.$this->producerFirstName.$this->producerMainName.')';
        return $base;
    }

    public function setDiscount($num){
        $this->discount = $num;
    }

    public function getPrice(){
        return $this->price - $this->discount;
    }

}

class CdProduct extends ShopProduct{
    public $playLength;
    public function __construct($title, $mainName, $firstName, $price,$playLength)
    {
        parent::__construct($title, $mainName, $firstName, $price);
        $this->playLength = $playLength;
    }

    function getPlayLength(){
        return $this->playLength;
    }

    public function getSummaryLine(){

        /*$base = $this->title.'('.$this->producerFirstName.$this->producerMainName.') : playing time'.$this->playLength;*/
        //调用父类的方法，不用重复写同样的代码
        $base = parent::getSummaryLine();
        $base .= ': playing time'.$this->playLength;
        return $base;
    }
}

class BookProduct extends ShopProduct{
    public $numPages;
    public function __construct($title, $mainName, $firstName, $price,$numPages)
    {
        parent::__construct($title, $mainName, $firstName, $price);
        $this->numPages = $numPages;
    }

    function getNumOfPages(){
        return $this->numPages;
    }

    public function getSummaryLine(){
        /*$base = $this->title.'('.$this->producerFirstName.$this->producerMainName.') : page count'.$this->numPages;*/
        //调用父类的方法，不用重复写同样的代码
        $base = parent::getSummaryLine();
        $base .= ':page count'.$this->numPages;
        return $base;
    }
}

class ShopProductWriter{
    private $shopProduct = array();
    public $aa;

    public function addProudct(ShopProduct $shopProduct){
        $this->shopProduct[] = $shopProduct;
    }

    public function write(){
        $str = '';
        foreach($this->shopProduct as $shopProduct){
            $str .= $shopProduct->title.':';
            $str .= $shopProduct->getProducer;
            $str .= '('.$shopProduct->getPrice.')';
        }
    }
}

$prod_class = new ReflectionClass('CdProduct');
/*Reflection::export($prod_class);*/

function classData(ReflectionClass $class){
    $details = '';
    $name = $class->getName();
    if($class->isUserDefined()){ //类是否在PHP代码中定义
        $details .= '';
    }
    if($class->isInternal()){ //是否是内置的类
        $details .= '';
    }
    if($class->isInterface()){//是否是接口
        $details .= '';
    }
    if($class->isAbstract()){//是否是抽象类
        $details .= '';
    }
    if($class->isFinal()){//是否可继承
        $details .= '';
    }
    if($class->isInstantiable()){//是否可以实例化
        $details .= '';
    }
}

class ReflectionUtil{
    static function getClassSource(ReflectionClass $class){
        $path = $class->getFileName();//获取到类文件的绝对路径
        var_dump($path);
        $lines = @file($path);
        $from = $class->getStartLine();
        $to = $class->getEndLine();
        $len = $to-$from+1;
        return implode(array_slice($lines,$from-1,$len));
    }

    static function getMethodSource(ReflectionMethod $method){
        $path = $method->getFileName();//获取到类文件的绝对路径
        var_dump($path);
        $lines = @file($path);
        $from = $method->getStartLine();
        $to = $method->getEndLine();
        $len = $to-$from+1;
        return implode(array_slice($lines,$from-1,$len));
    }
}

/*print_r(ReflectionUtil::getClassSource($prod_class));*/

/*$methods = $prod_class->getMethods();

foreach($methods as $method){
    print methodData($method);
    print "\n----\n";
}*/


function methodData(ReflectionMethod $method){
    $details = '';
    $name = $method->getName();

    if($method->isUserDefined()){ //方法是否在PHP代码中定义
        $details .= '';
    }
    if($method->isInternal()){ //是否是内置的类
        $details .= '';
    }
    if($method->isAbstract()){//是否是抽象类
        $details .= '';
    }
    if($method->isPublic()){//是否是公开

    }
    if($method->isProtected()){//是否是受保护

    }
    if($method->isPrivate()){//是否是私有的

    }
    if($method->isStatic()){//是否是静态的

    }
    if($method->isFinal()){//是否可继承
        $details .= '';
    }
    if($method->isConstructor()){//是不是类的构造方法
        $details .= '';
    }
    if($method->returnsReference()){//是否返回引用
        $details .= '';
    }
}

/*print_r(ReflectionUtil::getMethodSource($prod_class->getMethod('getSummaryLine')));*/

$method = $prod_class->getMethod('getSummaryLine');
$params = $method->getParameters();
foreach($params as $param){
    print argData($param);
}

function argData(ReflectionParameter $arg){
    $details = '';
    $declaringclass = $arg->getDeclaringClass();
    $name = $arg->getName();
    $class = $arg->getClass();
    $position = $arg->getPosition();

    if(!empty($class)){ //
        $className = $class->getName();
    }
    var_dump($class);
    if($arg->isPassedByReference()){ //参数是否为引用
        $details .= '';
    }
    if($arg->isDefaultValueAvailable()){//是否有默认值
        $details .= '';
    }
}